//
//  HomeHeartRateViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/8.
//

import UIKit
import RxSwift
import RxCocoa
import RxGesture
import MYModule

open class HomeHeartRateViewModel: HomeBaseViewModel {
    
    // 最后记录的显示日期
    var date: Date?
    
    public let UIReplay: BehaviorRelay<HomeHeartRateModel>
    
    public init(date: Date) {
        
        let model = HomeHeartRateModel()
        self.date = date
        model.date = Self.dateStr(date: date)
        UIReplay = BehaviorRelay<HomeHeartRateModel>(value: model)
        
        super.init(cellType: .heartRate)
        
        reloadData(date)
        
    }
    
    public override func reloadData(_ date: Date? = nil) {
        
        reloadFullDayData(date: date)
        
        reloadMeasureState()
        
        reloadSingleData()
        
        showDateStr(date: date)
        
       
    }
    
    
    public func reloadFullDayData(date: Date?) {
        
        guard let date = date else {
            return
        }

        let dataModel = HeartRateChartDataModel(date: date)
        
        let model = UIReplay.value
        
        model.chartMinData = dataModel.chartMinData
        model.chartMaxData = dataModel.chartMaxData
        model.chartDatas = dataModel.chartDatas
        model.chartYAxisLineCount = dataModel.chartYAxisCount
        model.hasData = dataModel.hasData
        
        UIReplay.accept(model)
        
    }
    
    public func showDateStr(date: Date?) {
        
        let model = UIReplay.value
        
        guard let date = date else {
            model.date = ""
            return
        }

        var fullDayDate: Date? = nil
        if let sqlModel = HeartRateModel.day(date: date) {
            fullDayDate = sqlModel.date
        }
        
        var singleDate: Date? = nil
        if let end = aSingleHeartRate()?.end {
            singleDate = Date(timeIntervalSince1970: end)
        }
        
        let lastDate = self.lastDate(fullDayDate: fullDayDate, singleDate: singleDate)
      
        if let lastDate = lastDate {
            model.date = Self.dateStr(date: lastDate)
        } else {
            model.date =  ""
        }
        
        self.date = lastDate
        
        UIReplay.accept(model)
       
    }
    
    @discardableResult
    public func reloadMeasureState() ->HomeHeartRateModel {
       
        let model = UIReplay.value
        if BluetoothManager.shared.deviceState == .connected {
            
            if BluetoothManager.shared.measuring == .heartRate {
                model.measureState = .selected
            } else {
                model.measureState = (BluetoothManager.shared.measuring != .none || BluetoothManager.shared.isBatterycharging) ? .disabled : .normal
            }
            
        } else {
            model.measureState = .disabled
        }
        return model
    }
    
    
    public func reloadSingleData() {
        
        let model = UIReplay.value
        guard let singleHeartRateModel = aSingleHeartRate() else {
            model.heartRate = 0
            UIReplay.accept(model)
            return
        }
        
        model.heartRate = singleHeartRateModel.heartRate
        
        UIReplay.accept(model)
    }
    
 
    public func automaticMeasure() {
        let model = UIReplay.value
        if model.measureState == .disabled {
            return
        }
        
        if model.measureState == .selected {
            measured()
        } else {
            measure()
        }
        
    }
    
    // 开始测量
    public func measure() {
        
        let model = UIReplay.value
        model.measureState = .selected
        
        BluetoothManager.shared.setStartSingleHR()
        
        print("已经在测量了")
        
    }
    
    // 结束测量
    public func measured() {
        
      
        if BluetoothManager.shared.measuring == .heartRate {
            BluetoothManager.shared.setStopSingleHR()
        }
        
        measuredData()
        print("已经结束测量了")
        
    }
    
    public func measuredData() {
        
        let model = UIReplay.value
        model.measureState = .normal
        reloadData(self.date)
       
        
    }
    
    
    
    // 查询单次测量心率
    public func aSingleHeartRate() -> SingleHeartRateModel? {
        
        guard let singleHeartRateModel = SingleHeartRateModel.last(from: Date().timeIntervalSince1970) else {
            return nil
        }
        
        return singleHeartRateModel
        
    }
    
   

}
